package com.heima.big_event03.config;

import com.heima.big_event03.Interceptor.myIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


@Component
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    //拦截器对象
    @Autowired
    myIntercepter myIntercepter;

    /**
     * 注册拦截器
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(myIntercepter)
                //<--------只拦截含有user,category,article词眼的网址
                .addPathPatterns("/user/**","/category/**","/article/**")
                //要忽略的路径
                .excludePathPatterns("/user/register","/user/login");

    }

    /**
     * 生成api文档
     * @return
     */
    @Bean
    public Docket docket1() {
        log.info("准备生成接口文档");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("大事件项目接口文档")
                .version("2.0")
                .description("大事件项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("后端接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.heima.big_event03.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 拓展springMVC框架的消息转化器
     * @param converters
     */
//    @Override
//    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//        //创建自己的信息转换器---???和ObjectMapper有什么联系？
//        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//        //添加属性JacksonObjectMapper
//        converter.setObjectMapper(new JacksonObjectMapper());
//        //提交到转换器集 --- 0:代表优先级最高
//        converters.add(0,converter);
//    }
}
